package com.example.stylecameraversion01;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.ImageView;
import android.widget.Toast;

public class ActivityGallery extends Activity {
	private static final int REQUEST_CODE = 1888;
	private String selectedImagePath;
	ImageView img_view, imgSave, imgShare;
	Bitmap photo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_edit_picture);
		init();
		handle();
		gallery();
	}

	private void init() {
		// TODO Auto-generated method stub
		img_view = (ImageView) findViewById(R.id.imageFrame);
		imgSave = (ImageView)findViewById(R.id.btnSave);
		imgShare = (ImageView)findViewById(R.id.btnShare);
	}

	private void handle() {
		// TODO Auto-generated method stub
		
	}

	public void gallery() {
		Intent localIntent = new Intent("android.intent.action.PICK");
		localIntent.setType("image/*");
		startActivityForResult(
				Intent.createChooser(localIntent, "Select Picture"),
				REQUEST_CODE);
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			if (requestCode == REQUEST_CODE) {
				Uri selectedImageUri = data.getData();
				selectedImagePath = getPath(selectedImageUri);
				try {
					FileInputStream fileis = new FileInputStream(
							selectedImagePath);
					BufferedInputStream bufferedstream = new BufferedInputStream(
							fileis);
					byte[] bMapArray = new byte[bufferedstream.available()];
					bufferedstream.read(bMapArray);
					photo = BitmapFactory.decodeByteArray(bMapArray, 0,
							bMapArray.length);
					img_view.setImageBitmap(photo);
					if (fileis != null) {
						fileis.close();
					}
					if (bufferedstream != null) {
						bufferedstream.close();
					}
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public String getPath(Uri uri) {
		String[] projection = { MediaStore.Images.Media.DATA };
		Cursor cursor = managedQuery(uri, projection, null, null, null);
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}
	private void SaveImage(Bitmap bitmap) {
		img_view.setDrawingCacheEnabled(true);
		bitmap = img_view.getDrawingCache();
		String root = Environment.getExternalStorageDirectory().toString();
		File newDir = new File(root + "/saved_images");
		newDir.mkdirs();
		Random gen = new Random();
		int n = 10000;
		n = gen.nextInt(n);
		String photoname = "Image-" + n + ".jpg";
		File file = new File(newDir, photoname);
		if (file.exists())
			file.delete();
		try {
			FileOutputStream out = new FileOutputStream(file);
			bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
			out.flush();
			out.close();
			Toast.makeText(getApplicationContext(), "Saved to your folder",
					Toast.LENGTH_SHORT).show();
		} catch (Exception e) {

		}
	}

}
